package org.piax.sample;

import org.piax.dtpubsub.DTPubSub;
import org.piax.dtpubsub.SubscribeListener;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

public class PIAXDTNSample extends Activity implements SubscribeListener {
    static DTPubSub dtp;
    private static final int MENU_EXIT = Menu.FIRST + 1;

    static final String TOPIC = "sample";
    EditText et;
    TextView tv;
    TextView lv;
    ScrollView sv;
    Handler handler;

    // Inner class for updating Views on the main thread.
    class TextUpdater implements Runnable {
        TextView tv;
        ScrollView sv;
        EditText et;
        String txt;
        public TextUpdater(TextView tv, ScrollView sv, String txt) {
            this.tv = tv;
            this.sv = sv;
            this.txt = txt;
        }
        public void run() {
            if (sv != null) {
                tv.append(txt + "\n");
                sv.post(new Runnable() {
                    public void run() {
                        sv.fullScroll(ScrollView.FOCUS_DOWN);
                    }
                });
            }
            else {
                tv.setText(txt);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, MENU_EXIT, Menu.NONE, "Exit")
            .setIcon(android.R.drawable.ic_menu_close_clear_cancel);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onMenuItemSelected(int fid, MenuItem item) {
        int id = item.getItemId();
        switch (id) {
        case MENU_EXIT:
            dtp.finish();
        	finish();
            return true;
        }
        return false;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        et = (EditText)findViewById(R.id.edit_text);
        sv = (ScrollView)findViewById(R.id.scroll_view);
        tv = (TextView)findViewById(R.id.text_view);
        lv = (TextView)findViewById(R.id.link_status);
        handler = new Handler();

        Button button = (Button)findViewById(R.id.add_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt = et.getText().toString();
                txt = Build.MODEL + ">" + txt;
                et.setText("");
                dtp.publish(TOPIC, txt.getBytes());
            }  
        });
        
        if (dtp == null) {
        	dtp = new DTPubSub(this);
        }
        dtp.subscribe(TOPIC, this);
    }
    
    @Override
    public void onReceive(String topic, byte[] data) {
        handler.post(new TextUpdater(tv, sv, new String(data)));
    }
}